package com.hxzy.projectssmplus.controller;

import com.hxzy.projectssmplus.dto.HouseNewDTO;
import com.hxzy.projectssmplus.entity.HouseNew;
import com.hxzy.projectssmplus.service.HouseNewService;
import com.hxzy.projectssmplus.service.SysAreaService;
import com.hxzy.projectssmplus.vo.AjaxResult;
import com.hxzy.projectssmplus.vo.HouseNewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2021/12/08 16:51
 * Description:
 * Version: V1.0
 */
@RestController
@RequestMapping(value = "/houseNew")
public class HouseNewController {

    @Autowired
    private HouseNewService houseNewService;

    @Autowired
    private SysAreaService sysAreaService;
    /**
     * 用于前端显示信息
     * @param houseNewDTO
     * @return
     */
    @GetMapping(value = "/list")
    public AjaxResult searchByParams(HouseNewDTO houseNewDTO){
        List<HouseNewVO> houseNewVOList = this.houseNewService.searchByParamsVO(houseNewDTO);
        return AjaxResult.success_data(houseNewVOList);
    }

    @PostMapping(value = "/insert")
    public AjaxResult insertHouseNew(@RequestBody HouseNew houseNew){
        houseNew.setLatitude(new BigDecimal(0));
        houseNew.setLongitude(new BigDecimal(0));
        houseNew.setCreateBy("admin");
        houseNew.setCreateTime(new Date());
        int save = this.houseNewService.insertHouseNew(houseNew);
        return 1>0?AjaxResult.success("新增成功"):AjaxResult.error("新增失败");
    }

    @PostMapping(value = "/update")
    public AjaxResult updateHouseNew(@RequestBody HouseNew houseNew){
        houseNew.setUpdateBy("admin");
        houseNew.setUpdateTime(new Date());
        boolean updateById = this.houseNewService.updateById(houseNew);
        return updateById?AjaxResult.success("修改成功"):AjaxResult.error("修改失败");
    }

    @GetMapping(value = "/delete")
    public AjaxResult deleteHouseNew(Long id){
        boolean removeById = this.houseNewService.removeById(id);
        return removeById?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }

    @GetMapping(value = "/searchAll")
    public AjaxResult searchAll(){
        List<HouseNew> list = this.houseNewService.list();
        return AjaxResult.success_data(list);
    }
}
